package de.hauschild.orbitvm.vm.instruction.impl.single;

import de.hauschild.orbitvm.vm.VM;
import de.hauschild.orbitvm.vm.instruction.impl.AbstractInstruction;

public class Cmpz extends AbstractInstruction {

  public enum CompareMode {
    LTZ, LEZ, EQZ, GEZ, GTZ
  }

  private final int argumentAdress;
  private final CompareMode compareMode;

  public Cmpz(final int theMyAdress, final int theArgumentAdress, final CompareMode theCompareMode) {
    super(theMyAdress);
    argumentAdress = theArgumentAdress;
    compareMode = theCompareMode;
  }

  @Override
  public void evaluate(final VM vm) {
    final double value = vm.getMemory()[argumentAdress];
    boolean result = vm.getStatus();
    switch (compareMode) {
    case LTZ:
      result = value < 0.0;
      break;
    case LEZ:
      result = value <= 0.0;
      break;
    case EQZ:
      result = value == 0.0;
      break;
    case GEZ:
      result = value >= 0.0;
      break;
    case GTZ:
      result = value > 0.0;
      break;
    }
    vm.setStatus(result);
  }

  @Override
  public String toString() {
    return String.format("%d: Cmpz (%s)", getInstructionCounter(), compareMode);
  }

}
